package com.android.mangalib.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import com.android.mangalib.activity.R;
import com.android.mangalib.object.Tome;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TomeForecastsAdapter extends BaseAdapter {
	
	List<Tome> collection;
	LayoutInflater inflater;
	

	public TomeForecastsAdapter(Context context, List<Tome> collection) {
		inflater = LayoutInflater.from(context);
		this.collection = collection;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return collection.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return collection.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TomeViewHolder holder;
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		
		if(convertView == null) {
			holder = new TomeViewHolder();
			
			// get the layout for printing the list of tome in the forecasts view
			convertView = inflater.inflate(R.layout.itemforecaststome, null);
			
			// get the xml objects
			holder.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
			holder.tvPrice = (TextView)convertView.findViewById(R.id.tvPrice);
			holder.tvDate = (TextView)convertView.findViewById(R.id.tvDate);
			convertView.setTag(holder);
		}
		else {
			holder = (TomeViewHolder)convertView.getTag();
		}
		
		// set the objects with their values
		holder.tvTitle.setText(collection.get(position).getTitle());
		holder.tvPrice.setText(String.valueOf(collection.get(position).getPrice()) + "€");
		holder.tvDate.setText(sdf.format(collection.get(position).getPublication_date()));
		
		return convertView;
	}
	
	public class TomeViewHolder {
		
		TextView tvTitle, tvPrice, tvDate, tvOwned;
	}

}
